package com.foodost.idiomer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class Settings extends Activity implements OnClickListener{
    
    private RadioButton grage1,grage2,grage3;
    private RadioButton play1,play2;
    private Button okBtn;
    private int grage,play;
    private int grageResId,playResId;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        initView();
        grage = Utils.getOrmGrage(this);
        play = Utils.getPlaysSetting(this);
        grageResId = initGrageResId(grage);
        playResId = initPlayResId(play);
    }
    
    private void initView(){
        grage1 = (RadioButton)findViewById(R.id.grage1);
        grage2 = (RadioButton)findViewById(R.id.grage2);
        grage3 = (RadioButton)findViewById(R.id.grage3);
        play1 = (RadioButton)findViewById(R.id.playnomal);
        play2 = (RadioButton)findViewById(R.id.playone);
        okBtn = (Button)findViewById(R.id.ok);
        
        grage1.setOnClickListener(this);
        grage2.setOnClickListener(this);
        grage3.setOnClickListener(this);
        play1.setOnClickListener(this);
        play2.setOnClickListener(this);
        okBtn.setOnClickListener(this);
        int palysSetting = Utils.getPlaysSetting(this);
        switch(palysSetting){
        case 1:
            play1.setChecked(true);
            break;
        case 2:
            play2.setChecked(true);
            break;
        }
        int grage = Utils.getOrmGrage(this);
        switch(grage){
        case 1:
            grage1.setChecked(true);
            break;
        case 2:
            grage2.setChecked(true);
            break;
        case 3:
            grage3.setChecked(true);
            break;
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.grage1){
            grage1.setChecked(true);
            grage2.setChecked(false);
            grage3.setChecked(false);
            setGrageResId(v.getId());
        }else if(v.getId() == R.id.grage2){
            grage1.setChecked(false);
            grage2.setChecked(true);
            grage3.setChecked(false);
            setGrageResId(v.getId());
        }else if(v.getId() == R.id.grage3){
            grage1.setChecked(false);
            grage2.setChecked(false);
            grage3.setChecked(true);
            setGrageResId(v.getId());
        }else if(v.getId() == R.id.playnomal){
            play1.setChecked(true);
            play2.setChecked(false);
            setPlayResId(v.getId());
        }else if(v.getId() == R.id.playone){
            play1.setChecked(false);
            play2.setChecked(true);
            setPlayResId(v.getId());
        }else if(v.getId() == R.id.ok){
        	newActivity();
        }
    }
    
    private void setGrageResId(int resId){
        grageResId = resId;
    }
    
    
    
    public int getGrageResId() {
        return grageResId;
    }
    
    public int initGrageResId(int grage){
        switch(grage){
        case 1:
            return R.id.grage1;
        case 2:
            return R.id.grage2;
        case 3:
            return R.id.grage3;
            default:
                return R.id.grage1;
        }
    }

    public int getPlayResId() {
        return playResId;
    }

    private void setPlayResId(int resId){
        playResId = resId;
    }
    
    public int initPlayResId(int play){
        switch(play){
        case 1:
            return R.id.playnomal;
        case 2:
            return R.id.playone;
            default:
                return R.id.playnomal;
        }
    }
    
    
    
    public void newActivity(){
        Utils.ormGrage(this,getGrageResId());
        Utils.setPlaysSetting(this, getPlayResId());
    	int grage = Utils.getOrmGrage(this);
    	int play = Utils.getPlaysSetting(this);
    	if(play == 1){
    		Intent in = new Intent();
    		in.setClass(this, MainActivity.class);
    		in.putExtra(Constants.GRAGE_INTENT, grage);
    		startActivity(in);
    	}else if(play == 2){
    		Intent in = new Intent();
    		in.setClass(this, CavilOneActivity.class);
    		in.putExtra(Constants.GRAGE_INTENT, grage);
    		startActivity(in);
    	}
    }

}
